Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Reduce synchronization in PendingTransactions #1447

Merged

Conversation

ajsutton
Copy link
Contributor

PR description

Reduce the amount of work PendingTransactions does inside synchronized blocks to reduce contention.

  • The hash to transaction map is now a ConcurrentHashMap allowing queries like getTransactionByHash to be performed without any need for synchronization.
  • Iterating to remove old transactions or find local transactions to send to peers can now be done without synchronized blocks except when an old transaction is actually being removed.
  • Adds and removes still require synchronization but notifying listeners is now done outside the synchronized block.

ajsutton added 3 commits May 15, 2019 09:57
…ransactions.

In particularly getTransactionByHash and notifying listeners of added or removed transactions are no longer in synchronized blocks.

pendingTransactions.values().stream()
.filter(transaction -> transaction.getAddedToPoolAt().isBefore(removeTransactionsBefore))
.forEach(transaction -> removeTransaction(transaction.getTransaction()));
Copy link
Contributor

@shemnon shemnon May 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if there was some way to assert that the underlying spliterator had the CONCURRENT characteristic before performing. While this is correct (because it is from a ConcurrentHashMap) I am concerned that future alterations may miss this nuance. At least a comment would help.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the map ever gets changed back to a non-concurrent map, all the places it's accessed from will be problematic. This is actually the place that kind of mistake will be most obvious because the unit tests will fail with ConcurrentModificationException. The others will just be subtle bugs in the face of multiple threads.

@ajsutton ajsutton merged commit 7b0a39a into PegaSysEng:master May 15, 2019
@ajsutton ajsutton deleted the notify-listeners-outside-synchronized branch May 15, 2019 04:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants